// src/main.js
import Vue from 'vue'

// element-ui 组件（加前缀 El）
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from './store'      // 引入 Vuex
import App from './App'
import router from './router'

Vue.config.productionTip = false
Vue.use(Element)


// 路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = !!localStorage.getItem('user');

  if (to.matched.some(r => r.meta.requiresAuth) && !isLogin) {
    next({ path: '/login', query: { redirect: to.fullPath } });
  } else {
    next();       // 已登录或不需要登录，继续跳转
  }
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,      // 注册 Vuex
  components: { App },
  template: '<App/>'
})
